#include "widget_listener.h"
#include <string>
#include <iostream>
#include <sstream>



connected_player::connected_player( unsigned int c, QWidget* parent, Qt::WindowFlags flags ): QWidget( parent, flags )
{
	clinet_id = c;

	QVBoxLayout* layout = new QVBoxLayout( this );

	
	name_l = new QLabel( this );
	name_l->setTextFormat( Qt::RichText );
	set_name( "", "" );
	layout->addWidget( name_l );


	status_l = new QLabel( this );
	status_l->setTextFormat( Qt::RichText );
	set_status( "Not connected." );
	layout->addWidget( status_l );


	error_l = new QLabel( this );
	error_l->setTextFormat( Qt::RichText );
	error_l->setWordWrap( true );
	set_error( "No errors." );
	layout->addWidget( error_l );


	this->setLayout( layout );
};



void connected_player::set_name( const string& n, const string& t )
{
	ostringstream sout;
	sout << "<font size=5>" << n << " - " << t << "</font>";
	name_l->setText( sout.str().c_str() );
};



void connected_player::set_status( const string& s )
{
	ostringstream sout;
	sout << "<font size=5>" << s << "</font>";
	status_l->setText( sout.str().c_str() );
};



void connected_player::set_error( const string& e )
{
	ostringstream sout;
	if ( e == "No errors." )
		sout << "<font size=5>" << e << "</font>";
	else
		sout << "<font size=5 color=\"#FF0000\">" << e << "</font>";
	error_l->setText( sout.str().c_str() );
};



listener_widget::listener_widget( QWidget* parent, Qt::WindowFlags flags ): QWidget( parent, flags )
{	
	connected_players = new connected_player*[ server_listener::get_instance()->server->max_clients ];

	layout = new QVBoxLayout( this );


	QWidget* title_w = new QWidget( this );
	QHBoxLayout* title_w_l = new QHBoxLayout( title_w );

	QLabel* title_l = new QLabel( title_w );
	title_l->setTextFormat( Qt::RichText );
	title_l->setText( "<b><font size=8>Listener status</font></b>" );
	title_w_l->addWidget( title_l );

	restart_b = new QPushButton( "Restart server", title_w );
	restart_b->setFixedWidth( 200 );
	title_w_l->addWidget( restart_b );
	connect( restart_b, SIGNAL( clicked() ), this, SLOT( restart_listener() ) );

	title_w->setLayout( title_w_l );
	layout->addWidget( title_w );


	layout->addSpacing( 20 );

	
	status_l = new QLabel( this );
	status_l->setTextFormat( Qt::RichText );
	show_status( "Loading status..." );
	layout->addWidget( status_l );


	layout->addSpacing( 20 );


	QLabel* clients_l = new QLabel( this );
	clients_l->setTextFormat( Qt::RichText );
	clients_l->setText( "<b><font size=8>Clients statuses</font></b>" );
	layout->addWidget( clients_l );

	for ( unsigned int i = 0; i < server_listener::get_instance()->server->max_clients; ++i )
	{
		layout->addSpacing( 10 );
		QLabel* client_l = new QLabel( this );
		client_l->setTextFormat( Qt::RichText );
		ostringstream sout;
		sout << "<b><font size=6>Client " << i << " status</font></b>";
		client_l->setText( sout.str().c_str() );
		layout->addWidget( client_l );

		connected_players[ i ] = new connected_player( i, this );
		layout->addWidget( connected_players[ i ] );
	}


	layout->addStretch();


	this->setLayout( layout );
	//this->setMaximumSize( 546, ( 176 + server_listener::get_instance()->server->max_clients * 143 ) );
};



void listener_widget::show_status( const string& s )
{
	ostringstream sout;
	sout << "<font size=6>" << s << "</font>";
	status_l->setText( sout.str().c_str() );
};



void listener_widget::restart_listener()
{
	server_listener::get_instance()->restart_listener();
};